package com.qyhl.school.school.vlog.play;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;
import android.util.Pair;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.Target;
import com.makeramen.roundedimageview.RoundedImageView;
import com.qyhl.school.R;
import com.qyhl.webtv.basiclib.utils.StringUtils;
import com.qyhl.webtv.commonlib.common.CommonUtils;
import com.qyhl.webtv.commonlib.entity.school.SchoolVlogBean;
import com.qyhl.webtv.module_microvideo.shortvideo.play.player.ListVideoView;
import com.qyhl.webtv.module_microvideo.shortvideo.play.player.OnItemChildClickListener;
import com.qyhl.webtv.module_microvideo.shortvideo.play.player.VideoUtils;


import java.util.ArrayList;
import java.util.List;

public class VlogVideoAdapter extends RecyclerView.Adapter {
    private List<SchoolVlogBean> dataList = new ArrayList<>();

    private Context mContext;

    private RecyclerView recyclerView;

    private OnItemChildClickListener<SchoolVlogBean> onItemChildClickListener;

    public void setOnItemChildClickListener(OnItemChildClickListener<SchoolVlogBean> onItemChildClickListener) {
        this.onItemChildClickListener = onItemChildClickListener;
    }

    public VlogVideoAdapter(Context context, RecyclerView recyclerView) {
        this.mContext = context;
        this.recyclerView = recyclerView;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_school_vlog_play, parent, false);
        return new VideoViewHolder(view);
    }

    private void fitVideoScaleType(VideoViewHolder viewHolder, int width, int height) {
        int contentWidth = recyclerView.getWidth();
        int contentHeight = recyclerView.getHeight();
        VideoUtils.ScaleType scaleType = VideoUtils.getImageCropType(new Pair<>(contentWidth, contentHeight),
                new Pair<>(width, height));
        if (scaleType == VideoUtils.ScaleType.CENTER_CROP) {
            viewHolder.sdvCover.setScaleType(ImageView.ScaleType.CENTER_CROP);
        } else if (scaleType == VideoUtils.ScaleType.FIT_CENTER) {
            viewHolder.sdvCover.setScaleType(ImageView.ScaleType.FIT_CENTER);
        }
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, @SuppressLint("RecyclerView") int position) {
        final SchoolVlogBean videoBean = dataList.get(position);
        final VideoViewHolder viewHolder = (VideoViewHolder) holder;

        Glide.with(mContext)
                .asBitmap()
                .load(videoBean.getLogo())
                .addListener(new RequestListener<Bitmap>() {
                    @Override
                    public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Bitmap> target, boolean isFirstResource) {
                        return false;
                    }

                    @Override
                    public boolean onResourceReady(Bitmap resource, Object model, Target<Bitmap> target, DataSource dataSource, boolean isFirstResource) {
                        int width = resource.getWidth();
                        int height = resource.getHeight();
                        fitVideoScaleType(viewHolder, width, height);
                        return false;
                    }
                })
                .into(viewHolder.sdvCover);

        String headIcon;
        if (videoBean.getAuthor() != null && videoBean.getAuthor().getLogo() != null) {
            headIcon = videoBean.getAuthor().getLogo();
        } else {
            headIcon = CommonUtils.getInstance().getUserLogo();
        }

        Glide.with(mContext)
                .load(headIcon)
                .apply(new RequestOptions().error(R.drawable.comment_head_default).placeholder(R.drawable.comment_head_default))
                .into(viewHolder.headIcon);

        if (videoBean.getAuthor() != null && videoBean.getAuthor().getNickName() != null) {
            viewHolder.nickName.setText(videoBean.getAuthor().getNickName());
        } else {
            String nickName = CommonUtils.getInstance().getNickName();
            viewHolder.nickName.setText(nickName);
        }

        viewHolder.summary.setText(videoBean.getInfo());
        if (videoBean.getTag() != null && StringUtils.isNotEmpty(videoBean.getTag().getName())) {
            viewHolder.tagName.setText("#" + videoBean.getTag().getName() + "#");
        }

        viewHolder.commentNum.setText(videoBean.getCommentCount() + "");
        viewHolder.likeNum.setText(videoBean.getLikeCount() + "");
        viewHolder.followNum.setText(videoBean.getShareCount() + "");

        viewHolder.headIcon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });

        viewHolder.tagName.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });

        viewHolder.videoLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (viewHolder.videoView != null) {
                    if (viewHolder.videoView.isPlaying()) {
                        viewHolder.videoView.pause();
                        viewHolder.videoTag.setVisibility(View.VISIBLE);
                    } else {
                        viewHolder.videoView.start();
                        viewHolder.videoTag.setVisibility(View.GONE);
                    }
                }
            }
        });

        if (videoBean.isLike()) {
            viewHolder.likeNum.setCompoundDrawablesWithIntrinsicBounds(null, ContextCompat.getDrawable(mContext, R.drawable.short_video_play_like_on_icon), null, null);
        } else {
            viewHolder.likeNum.setCompoundDrawablesWithIntrinsicBounds(null, ContextCompat.getDrawable(mContext, R.drawable.short_video_play_like_off_icon), null, null);
        }

        viewHolder.commentNum.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemChildClickListener.onClickListener(viewHolder.commentNum, videoBean, position);
            }
        });


        viewHolder.likeNum.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean isFollow;
                if (viewHolder.likeNum.getCompoundDrawables()[1].getConstantState().equals(mContext.getResources().getDrawable(R.drawable.short_video_play_like_on_icon).getConstantState())) {
                    isFollow = true;
                } else {
                    isFollow = false;
                }
                onItemChildClickListener.onClickListener(viewHolder.likeNum, videoBean, position, isFollow);
            }
        });

        viewHolder.followNum.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemChildClickListener.onClickListener(viewHolder.followNum, videoBean, position);
            }
        });
    }

    public void addData(List<SchoolVlogBean> newDataList) {
        this.dataList.addAll(newDataList);
    }

    public void setData(List<SchoolVlogBean> newDataList) {
        this.dataList.clear();
        this.dataList.addAll(newDataList);
        notifyDataSetChanged();
    }

    public SchoolVlogBean getDataByPosition(int position) {
        return dataList.get(position);
    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    public class VideoViewHolder extends RecyclerView.ViewHolder {
        public ListVideoView videoView;
        public ImageView sdvCover, videoTag;
        RoundedImageView headIcon;
        public TextView nickName, summary, tagName, commentNum, followNum, likeNum;
        public ProgressBar progressBar;
        RelativeLayout videoLayout;


        VideoViewHolder(View itemView) {
            super(itemView);
            videoView = itemView.findViewById(R.id.video_view);
            sdvCover = itemView.findViewById(R.id.video_cover);
            headIcon = itemView.findViewById(R.id.head_icon);
            progressBar = itemView.findViewById(R.id.progress_bar);
            nickName = itemView.findViewById(R.id.nickName);
            summary = itemView.findViewById(R.id.summary);
            tagName = itemView.findViewById(R.id.tagName);
            commentNum = itemView.findViewById(R.id.comment_num);
            followNum = itemView.findViewById(R.id.follow_num);
            likeNum = itemView.findViewById(R.id.like_num);
            videoTag = itemView.findViewById(R.id.video_tag);
            videoLayout = itemView.findViewById(R.id.video_layout);
        }
    }
}
